<?php

/**
 * 用户任务类
 *
 * @author Administrator
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use think\log;
use think\Db;

class Usertask extends Backend {

    protected $relationSearch = true;
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('Usertask');
    }

    public function index() {

        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with('task')
                    ->where('usertask.status=2')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->with('task')
                    ->where($where)
                    ->where('usertask.status=2')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->fetch();
    }

    /*
     * 查看提交信息
     */

    public function see() {
        $utask_id = $this->request->param('ids');

        $modeld = model('Auditing');
        $list = $modeld
                ->where('utask_id', $utask_id)
                ->select();
        $this->view->assign('list', $list);
        return $this->fetch();
    }

    /*
     * 审核成功
     */

    public function adopt() {
        $utask_id = $this->request->param('ids');
      
            $task = $this->model->get($utask_id);
            $extension = model('extension')->get(2);
            if(!$extension||!$task)
            {
                 $this->error('出现异常，请刷新重试');
            }
           
             $tim=time();
      
            $money=$task->commission;
             $user_id=$task->user_id;
             $task_id=$task->task_id;
             $sqltend='';
              $user=model('user');
              $tasksql="update rwz_task set number=number+1  where id=$task_id";
               $usesql="update rwz_usertask set status=99 where id=$utask_id";
             if($extension->status)
             {
                 $onefee=$extension->first* $money/100;
                 $twofee=$extension->twice* $money/100;
                 $tasksql="update rwz_task set number=number+1,onefee=onefee+$onefee,twofee=twofee+$twofee where id=$task_id";
                 $usesql="update rwz_usertask set status=99,onefee=$onefee,twofee=$twofee,updatetime=$tim where id=$utask_id";
                  $users = $user->get($user_id);
                  if($users->pid && $extension->first>0)
                  {
                               $sqlu1="update rwz_user set user_money=user_money+$onefee,onefee=onefee+$onefee where id=$users->pid";
   
                      $sqltend=",(1,$onefee,$tim,$users->pid,$task_id,$user_id,1)";
                      $ppuser = $user->get($users->pid);
                      if($ppuser->pid && $extension->twice>0)
                      {
                            $sqltend.=",(1,$twofee,$tim,$ppuser->pid,$task_id,$users->pid,2)";
                             $sqlu2="update rwz_user set user_money=user_money+$twofee,twofee=twofee+$twofee where id=$ppuser->pid";
                      }
                      
                  }
                  
                 
             }
         
           $sqlu="update rwz_user set user_money=user_money+$money ,commission=commission+$money where id=$user_id";//完成任务余额增加，佣金累计
 
            $temsql="insert into rwz_stream (type ,money,createtime,user_id,task_id,son_id,level)values(1,$money,$tim,$user_id,$task_id,0,0)". $sqltend;
            
              Db::startTrans();
            try {
             $dd=Db::execute($sqlu);
            if($dd)
            {
                if(isset($sqlu1))
                {
                    Db::execute($sqlu1);
                }
               if(isset($sqlu2))
                {
                    Db::execute($sqlu2);
                }
                Db::execute($temsql);
            }
           
             Db::execute($tasksql);
             Db::execute($usesql);
            
                // 提交事务
                Db::commit();
            
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('出现异常，请刷新重试');
            }

       $this->success('成功');
   
        
    }

    /*
     * 审核不通过
     */

    public function fail() {
        $utask_id = $this->request->param('ids');
        if ($this->request->isPost()) {
            $data = $this->model->get($utask_id);
            Db::startTrans();
            try {
                $dat = $this->model->where('id', $utask_id)->update(['status' => 3]);
                if ($dat) {
                    $da = model('task')->where('id', $data->task_id)->setDec('accept');
                }

                // 提交事务
                Db::commit();
                $result = [
                    'code' => 1,
                    'msg' => '',
                    'data' => '',
                    'url' => '',
                    'wait' => '',
                ];
                echo json_encode($result);
                exit;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('出现异常，请刷新重试');
            }
        }

        return $this->fetch();
    }

}
